package org.example.tialsproject.controller;

import org.example.tialsproject.pojo.Emp;
import org.example.tialsproject.pojo.ResponseResult;
import org.example.tialsproject.pojo.User;
import org.example.tialsproject.service.EmpService;

import org.example.tialsproject.utils.JwtUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController()
@RequestMapping("/user")
public class UserController {
    @Autowired
    public EmpService empService;

    /*登录的方法*/
    @PostMapping("/login")
    public ResponseResult userLogin(@RequestBody User user) {
        Emp emp = empService.userLogin(user);
        if (emp != null) {
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("id", emp.getId());
            map.put("name", emp.getName());
            map.put("username", emp.getUsername());
            map.put("password", emp.getPassword());
            String jwt = JwtUtils.renderJwt(map);
            return ResponseResult.success(jwt);
        } else {
            return ResponseResult.error( "用户名或者密码不存在");
        }
    }
}
